﻿var zombies = zombies || {};

zombies.gallery = function () {
    var self = this;
    self.Gallery = ko.observableArray();
    self.Advertises = ko.observableArray();

    self.SelectedAd = ko.observable();

    self.gallery1 = ko.computed(function () {
        return ko.utils.arrayFilter(self.Gallery(), function (item) {
            return (self.Gallery().indexOf(item) % 4) == 0;
        });
    });
    self.gallery2 = ko.computed(function () {
        return ko.utils.arrayFilter(self.Gallery(), function (item) {
            return (self.Gallery().indexOf(item) % 4) == 1;
        });
    });
    self.gallery3 = ko.computed(function () {
        return ko.utils.arrayFilter(self.Gallery(), function (item) {
            return (self.Gallery().indexOf(item) % 4) == 2;
        });
    });
    self.gallery4 = ko.computed(function () {
        return ko.utils.arrayFilter(self.Gallery(), function (item) {
            return (self.Gallery().indexOf(item) % 4) == 3;
        });
    });

    self.doCheck = function () {
        var setting = {
            url: "/api/Publisher/PostGallery/" + galleryModel.SelectedAd(),
            method: "POST",
            allowedTypes: "jpg,png,gif",
            fileName: "myfile",
            multiple: true,
            onSuccess: function (files, data, xhr) {
                var item = {
                    AdvId: '',
                    Link: data
                };
                self.Gallery.push(new zombies.Gallery(item));
            },
            onError: function (files, status, errMsg) {
                alert("Tải lên thất bại");
            }
        }
        $("#galleryUpload").uploadFile(setting);
    };

    $.ajax({
        type: "GET",
        url: "/api/Publisher/GetGallery",
        contentType: "application/json",
        dataType: 'json'
    }).done(function (items) {
        for (var item in items) {
            if (items.hasOwnProperty(item)) {
                self.Gallery.push(new zombies.Gallery(items[item]))
            }
        }
    }).fail(function () {
        toastr.error('Lỗi bất thường');
    });
};

zombies.Gallery = function (item) {
    var self = this;
    self.AdvId = item.AdvId;
    self.Link = item.Link;
}

var loadAdvertise = function (page, elementQ, oderBy) {
    galleryModel.Advertises.removeAll();
    $.ajax({
        type: "GET",
        url: "/api/Publisher/GetAdvByCurrentPub",
        data: { page: page, elementQ: elementQ, orderBy: oderBy },
        contentType: "application/json",
        dataType: 'json'
    }).done(function (items) {
        for (var item in items) {
            if (items.hasOwnProperty(item)) {
                galleryModel.Advertises.push(new zombies.Advertise(items[item]));
            }
        }
    }).fail(function () {
        toastr.error('Lỗi bất thường');
    });
};


var galleryModel;
$(document).ready(function () {
    galleryModel = new zombies.gallery();
    loadAdvertise(1, 1111, 0);
    ko.applyBindings(galleryModel, document.getElementById("content_publisher"));

});
